﻿using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Logging;
using Microsoft.AspNetCore.Http;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using ViewModels;
using LogForParameter.Api.SwaggerHelp;
using System.IO;
using Microsoft.AspNetCore.Authorization;
using Library.Common;
using Library.GlobalVars;
using LogForParameter.IServices;
using Library.Helper;
using PaddleOCRSharp;
using System.Data;
using System.Drawing;
using Library.Commom.Redis;
using CoreMembers.FileHelp;

namespace LogForParameter.Api.Controllers
{
    /// <summary>
    /// 测试
    /// </summary>
    [CustomRoute(CustomApiVersion.MyApiVersion.FileSystem)]
    [ApiController]
    [AllowAnonymous]
    //[Route("api/[controller]/[action]")]
    public class FileProcess : ControllerBase
    {
        private static readonly string[] Summaries = new[]
        {
            "Freezing", "Bracing", "Chilly", "Cool", "Mild", "Warm", "Balmy", "Hot", "Sweltering", "Scorching"
        };
        private readonly ILogger<FileProcess> _logger;
        //private readonly IRedisBasketRepository _redisBasketRepository;
        /// <summary>
        /// 构造函数方式实现
        /// </summary>
        /// <param name="logger"></param>
        public FileProcess(ILogger<FileProcess> logger/*, IRedisBasketRepository redisBasketRepository*/)
        {
            _logger = logger;
            //_redisBasketRepository = redisBasketRepository;
        }
        /// <summary>
        /// 获取第一条日志信息
        /// </summary>
        /// <returns></returns>
        //[ApiExplorerSettings(IgnoreApi =true)]
        //[Obsolete]废弃的接口
        [CustomRoute(CustomApiVersion.MyApiVersion.FileSystem, actionName: "Get1")]
        [HttpGet]
        public OutPut Get()
        {
            //_logger.LogTrace("trace");
            //_logger.LogDebug("debug");
            //_logger.LogInformation("info");
            //_logger.LogWarning("warn");
            //_logger.LogError("error");
            //_logger.LogCritical("critical");
            //using (_logger.BeginScope("测试日志的Scope"))
            //{
            //    _logger.LogTrace("scope-trace");
            //    _logger.LogDebug("scope-debug");
            //    _logger.LogInformation("scope-info");
            //    _logger.LogWarning("scope-warn");
            //    _logger.LogError("scope-error");
            //    _logger.LogCritical("scope-critical");
            //}
            //_logger.LogTrace("Scope已经结束了。。。");
            //var OP = _iYBOrderTip.GetPubLicApiLog();
            string file = Path.Combine("D:\\", "2021年08月12日2021年7月用料需求单.docx");
            NPOIHelpW.ReadTest(file);
            return Public.NewOP_Ok("好的");
        }
        /// <summary>
        /// 获取数据
        /// </summary>
        /// <param name="input">post集合</param>
        /// <returns></returns>
        [CustomRoute(CustomApiVersion.MyApiVersion.FileSystem, actionName: "Get1")]
        [HttpPost]
        public OutPut Post(List<string> input)
        {
            string hosId = GetHeader.GetHosId(Request.Headers);
            string depId = GetHeader.GetDepId(Request.Headers);
            string jobnumber = GetHeader.GetUserid(Request.Headers);
            var result = NPOIHelpW.Test();
            return Public.NewOP_Ok(result);
        }
        /// <summary>
        /// 导入excel
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        public OutPut ImportExcelByColumnOrder()
        {
            var fileName = HttpContext.Request.Form.Files[0].FileName;
            var stream = this.HttpContext.Request.Form.Files[0].OpenReadStream();
            var result = NPOIHelpE.ExcelToDataTableFormStream(stream, fileName, "误删库存补录2020-10-29");
            return Public.NewOP_Ok("");
        }
        /// <summary>
        /// 图像文字识别
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        public OutPut OCRTextTest(IFormFile formFile)
        {
            if (formFile is null)
            {
                throw new ArgumentNullException(nameof(formFile));
            }
            Stream stream = formFile.OpenReadStream();
            byte[] imageByte = new byte[stream.Length];
            stream.Read(imageByte, 0, imageByte.Length);
            stream.Seek(0, SeekOrigin.Begin);
            stream.Close();
            PaddleOCRSharp.PaddleOCREngine paddleOCREngine = OCRHelp.InitORC();
            PaddleOCRSharp.OCRResult ocrResult = paddleOCREngine.DetectText(imageByte);
            string oCRResultText = ocrResult.Text;
            string detailsText = "";
            foreach (var item in ocrResult.TextBlocks)
            {
                detailsText += (item.ToString() + "\n");
            }
            //此段代码为winform项目，windows环境可用，仅解释了ocrresult的boxpoints的用法.
            //foreach (var item in ocrResult.TextBlocks)
            //{
            //    using (Graphics g = Graphics.FromImage(bitmap))
            //    {
            //        g.DrawPolygon(new Pen(Brushes.Red, 2), item.BoxPoints.Select(x => new PointF() { X = x.X, Y = x.Y }).ToArray());
            //    }
            //}

            OutPut op = Public.NewOP_Ok($"{oCRResultText}");
            op.ResultData.Add(ocrResult);
            return op;
        }
        #region PDF
        /// <summary>
        /// 生成pdf在D:\\pdf\\QuestPDF.pdf
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        public OutPut GeneratePdfTest()
        {
            QuestPDFUtils.GeneratePDF();
            return Public.NewOP_Ok("生成成功");
        }
        #endregion
    }
}
